/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.entity;

import exceptions.IdentifiantInvalide;
import exceptions.InstanceInvalide;
import javax.ejb.Remote;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author François
 */
@Entity
@Remote(ProductRemote.class)
@Table(name = "product")
@NamedQueries({ @NamedQuery(name = "Product.getAll", query = "SELECT p FROM Product p") })
public class Product implements ProductRemote {
    /**
     * Default serial version UID.
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "reference_product")
    private Long reference;
    
    @Column(name = "name_product", length = 255)
    private String name;
    
    @Column(name = "price_product")
    private double price;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ProductStore_product")
    private ProductStore productStore;
    
    @Column(name = "url_image_product")
    private String urlImage;
    
    @Column(name = "description_product")
    private String description;
    
    public Product() {
    }

    public Product(Long reference, String name, double price,
	    ProductStore productStore, String urlImage) {
	super();
	this.reference = reference;
	this.name = name;
	this.price = price;
	this.productStore = productStore;
	this.urlImage = urlImage;
    }
    
    public static void verifierReference(Long reference)
	    throws IdentifiantInvalide {
	/*
	 * Identifiant renseigné
	 */
	if (reference == null) {
	    throw new IdentifiantInvalide(
		    "L'identifiant du Magasin est obligatoire. null");
	}
    }
    
    public static void verifierInstance(ProductRemote produit)
	    throws InstanceInvalide, IdentifiantInvalide {
	if (produit != null) {
	    /*
	     * Vérification référence
	     */
	    Product.verifierReference(produit.getReference());

	    /*
	     * Vérification magasin
	     */
	    if (produit.getProductStore() == null) {
		throw new InstanceInvalide("Le Magasin du Produit est nul");
	    }
	} else {
	    throw new InstanceInvalide("L'instance de Produit est nulle");
	}
    }

    @Override
    public Long getReference() {
        return reference;
    }

    @Override
    public void setReference(Long id) {
        this.reference = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public double getPrice() {
        return price;
    }

    @Override
    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String getUrlImage() {
        return urlImage;
    }

    @Override
    public void setUrlImage(String urlImage) {
        this.urlImage = urlImage;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }
    
    @Override
    public ProductStore getProductStore() {
        return productStore;
    }

    @Override
    public void setProductStore(ProductStoreRemote productStore) {
        this.productStore = (ProductStore) productStore;
    }

    @Override
    public int hashCode() {
	int prime = 31;
	int result = 1;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	result = prime
		* result
		+ ((productStore == null || productStore.getReference() == null) ? 0
			: productStore.getReference().hashCode());
	long temp;
	temp = Double.doubleToLongBits(price);
	result = prime * result + (int) (temp ^ (temp >>> 32));
	result = prime * result
		+ ((reference == null) ? 0 : reference.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (!(obj instanceof Product)) {
	    return false;
	}
	Product other = (Product) obj;
	if (name == null) {
	    if (other.name != null) {
		return false;
	    }
	} else if (!name.equals(other.name)) {
	    return false;
	}
	if (productStore == null) {
	    if (other.productStore != null) {
		return false;
	    }
	} else if (productStore.getReference() == null) {
	    if (other.productStore.getReference() != null) {
		return false;
	    }
	} else if (!productStore.getReference().equals(other.productStore.getReference())) {
	    return false;
	}
	if (Double.doubleToLongBits(price) != Double
		.doubleToLongBits(other.price)) {
	    return false;
	}
	if (reference == null) {
	    if (other.reference != null) {
		return false;
	    }
	} else if (!reference.equals(other.reference)) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	return "Produit [reference=" + reference + ", name=" + name
		+ ", price=" + price + ", productStore=" + productStore.getReference()
		+ "]";
    }
    
}
